package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/21 6:27 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0640 {

    public String solveEquation5(String equation) {

//        String[] sides = equation.split("=");
//
//        String[] leftSide = sides[0].split("[+-]");
//
//        String[] rightSide = sides[1].split("[+-]");




//        int[] xIndex = new int[equation.length()];
//
//        for (int i = 0; i < xIndex.length; i++) {
//            xIndex[i] = -1;
//        }
//
//        List<Integer> xList = new ArrayList<>();
//        List<Integer> plusList = new ArrayList<>();
//        List<Integer> subList = new ArrayList<>();

        int xPerfix = 0;
        int constantPerfix = 0;

        int equalIndex = -1;

        int lastSymbol = -1;

        char[] chars = equation.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == 'x') {
//                if (lastSymbol == -1) {
//                    lastSymbol = 0;
//                }
                if (i == lastSymbol+1) {
                    if (equalIndex == -1) {
                        xPerfix += 1;
                    } else {
                        xPerfix -= 1;
                    }

                }else {
                    String perfix = equation.substring(lastSymbol, i);
//                    xList.add(Integer.valueOf(perfix));
                    if (equalIndex == -1) {
                        xPerfix += Integer.valueOf(perfix);
                    } else {
                        xPerfix -= Integer.valueOf(perfix);
                    }
                }
                lastSymbol = i;
            }

            if (chars[i] == '+') {
//                if (lastSymbol == -1) {
//                    lastSymbol = 0;
//                }

                lastSymbol = i;
//                if (lastSymbol == -1) {
//                    lastSymbol = 0;
//                }
                if (i == lastSymbol+1) {
//                    subList.add(1);
                    if (equalIndex == -1) {
                        constantPerfix += 1;
                    } else {
                        constantPerfix -= 1;
                    }
                }else {
                    String perfix = equation.substring(lastSymbol, i);
//                    subList.add(Integer.valueOf(perfix));

                    if (equalIndex == -1) {
                        constantPerfix -= Integer.valueOf(perfix);
                    } else {
                        constantPerfix += Integer.valueOf(perfix);
                    }
                }
                lastSymbol = i;


            }

            if (chars[i] == '=') {
                equalIndex = i;
                lastSymbol = i;
            }
        }

        return "x=" + constantPerfix/xPerfix;
    }


    public String solveEquation2(String equation) {

        int xPerfix = 0;
        int constantPerfix = 0;

        int equalIndex = -1;

        int lastSymbol = -1;

        char[] chars = equation.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == 'x') {
                if (i == lastSymbol+1) {
                    if (equalIndex == -1) {
                        xPerfix += 1;
                    } else {
                        xPerfix -= 1;
                    }

                }else {
                    String perfix = equation.substring(lastSymbol, i);
                    if (equalIndex == -1) {
                        xPerfix += Integer.valueOf(perfix);
                    } else {
                        xPerfix -= Integer.valueOf(perfix);
                    }
                }
                lastSymbol = i;
            } else

            if (chars[i] == '+') {
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1, i);
                    if (chars[lastSymbol] == '+') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else if (chars[lastSymbol] == '=') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }



                }
                lastSymbol = i;
            } else if (chars[i] == '-') {
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1, i);

                    if (chars[lastSymbol] == '+') {

                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }
                }
                lastSymbol = i;
            } else if (chars[i] == '=') {
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1, i);

                    if (chars[lastSymbol] == '+') {

                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }
                }
                equalIndex = i;
                lastSymbol = i;
            } else {

            }

            if (i == chars.length - 1) {
                String perfix = equation.substring(lastSymbol + 1);
                if (chars[lastSymbol] == '+') {
                    if (equalIndex == -1) {
                        constantPerfix -= Integer.valueOf(perfix);
                    } else {
                        constantPerfix += Integer.valueOf(perfix);
                    }
                } else if (chars[lastSymbol] == '=') {
                    if (equalIndex == -1) {
                        constantPerfix -= Integer.valueOf(perfix);
                    } else {
                        constantPerfix += Integer.valueOf(perfix);
                    }
                } else {
                    if (equalIndex == -1) {
                        constantPerfix += Integer.valueOf(perfix);
                    } else {
                        constantPerfix -= Integer.valueOf(perfix);
                    }
                }
            }
        }

        if (xPerfix == 0) {
            return "Infinite solutions";
        }





        return "x=" + constantPerfix/xPerfix;
    }


    // 从下一个处理位置角度去考虑
    public String solveEquationNew(String equation) {
        int xPerfix = 0;
        int constantPerfix = 0;
        int equalSymbol = 1;
        int lastSymbol = 0;

        char[] chars = equation.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '=') {
                if (lastSymbol < i) {
                    constantPerfix += Integer.valueOf(equation.substring(lastSymbol, i)) * equalSymbol;
                }
                equalSymbol = -1;
                lastSymbol = i + 1;
            } else if (chars[i] == 'x') {
                if (lastSymbol == i || chars[i - 1] == '+') {
                    xPerfix += equalSymbol;
                } else if (chars[i - 1] == '-') {
                    xPerfix -= equalSymbol;
                } else {
                    xPerfix += Integer.valueOf(equation.substring(lastSymbol, i)) * equalSymbol;
                }
                lastSymbol = i + 1;
            } else if (chars[i] == '+' || chars[i] == '-') {
                if (lastSymbol < i) {
                    constantPerfix += Integer.valueOf(equation.substring(lastSymbol, i)) * equalSymbol;
                }
                lastSymbol = i;
            }
        }

        if (xPerfix == 0 && constantPerfix != 0) {
            return "No solution";
        }

        if (xPerfix == 0 && constantPerfix == 0) {
            return "Infinite solutions";
        }


        return "x=" + (-constantPerfix/xPerfix);
    }



    public String solveEquationOld(String equation) {
        int xPerfix = 0;
        int constantPerfix = 0;
        int equalIndex = 1;
        int lastSymbol = 0;

        char[] chars = equation.toCharArray();
        for (int i = 0; i < chars.length; i++) {

            if (chars[i] == 'x') {
                if (i == lastSymbol+1) {
                    if (equalIndex == -1) {
                        xPerfix += 1;
                    } else {
                        xPerfix -= 1;
                    }
                }else {
                    if (lastSymbol == -1) {
                        lastSymbol = 0;
                    }
                    String perfix = equation.substring(lastSymbol, i);
                    if (equalIndex == -1) {
                        xPerfix += Integer.valueOf(perfix);
                    } else {
                        xPerfix -= Integer.valueOf(perfix);
                    }
                }
                lastSymbol = i;
            } else if (chars[i] == '+') {
                if (lastSymbol == -1) {
                    lastSymbol = 0;
                }
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1, i);
                    if (chars[lastSymbol] == '+') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else if (chars[lastSymbol] == '=') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }

                }
                lastSymbol = i;
            } else if (chars[i] == '-') {
                if (lastSymbol == -1) {
                    lastSymbol = 0;
                    continue;
                }
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1, i);
                    if (chars[lastSymbol] == '+') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }
                }
                lastSymbol = i;
            } else if (chars[i] == '=') {
                if (lastSymbol == -1) {
                    lastSymbol = 0;
                }
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1, i);

                    if (chars[lastSymbol] == '+') {

                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }
                }
                equalIndex = i;
                lastSymbol = i;
            } else {

            }

            if (i == chars.length - 1) {
                if (lastSymbol == -1) {
                    lastSymbol = 0;
                }
                if (chars[lastSymbol] != 'x') {
                    String perfix = equation.substring(lastSymbol + 1);
                    if (chars[lastSymbol] == '+') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else if (chars[lastSymbol] == '=') {
                        if (equalIndex == -1) {
                            constantPerfix -= Integer.valueOf(perfix);
                        } else {
                            constantPerfix += Integer.valueOf(perfix);
                        }
                    } else {
                        if (equalIndex == -1) {
                            constantPerfix += Integer.valueOf(perfix);
                        } else {
                            constantPerfix -= Integer.valueOf(perfix);
                        }
                    }
                }
            }
        }

        if (xPerfix == 0 && constantPerfix != 0) {
            return "No solution";
        }

        if (xPerfix == 0) {
            return "Infinite solutions";
        }


        return "x=" + constantPerfix/xPerfix;
    }
    // 从下一个处理位置角度去考虑

    public String solveEquation6(String equation) {

        char[] charArray = equation.toCharArray();

        int leftBorder = 0;
        for (int i = 0; i < charArray.length; i++) {

            char element = charArray[i];
            if (element == '+' || element == '-') {


            }

        }

        return "";
    }

    public String solveEquation(String equation) {

        int coff = 0;
        int sum = 0;
        int index = 0;
        int sign = 1;

        char[] charArray = equation.toCharArray();

        for (int i = 0; i < charArray.length; i++) {
            char element = charArray[i];
            if (element == '=') {
                if (index < i) {
                    sum += Integer.valueOf(equation.substring(index, i))* sign;
                }
                sign = -1;
                index = i + 1;
            } else if (element == 'x') {
                if (index == i || charArray[i - 1] == '+') {
                    coff += sign;
                } else if (charArray[i - 1] == '-') {
                    coff -= sign;
                }else{
                    coff += Integer.valueOf(equation.substring(index, i)) * sign;
                }
                index = i + 1;
            } else if (element == '+' || element == '-') {
                if (index < i) {
                    sum += Integer.valueOf(equation.substring(index, i)) * sign;
                }
                index = i;
            }
        }
        if (index < equation.length()) {
            sum += Integer.valueOf(equation.substring(index)) * sign;
        }
        if (coff == 0 && sum != 0) {
            return "No solution";
        }
        if (coff == 0 && sum == 0) {
            return "Infinite solutions";
        }
        return "x=" + (-sum/coff);

    }


    public static void main(String[] args) {
        LeetCode0640 leetCode0640 = new LeetCode0640();
//        System.out.println(leetCode0640.solveEquation("x+5-3+x=6+x-2"));
//        System.out.println(leetCode0640.solveEquation("x+5-3+x=6+x-2"));
//        System.out.println(leetCode0640.solveEquation("x=x"));
//        System.out.println(leetCode0640.solveEquation("2x=x"));
//        System.out.println(leetCode0640.solveEquation("x=x+2"));
//        System.out.println(leetCode0640.solveEquation("-x=-1"));


        System.out.println(leetCode0640.solveEquationNew("x+5-3+x=6+x-2"));
        System.out.println(leetCode0640.solveEquationNew("x=x"));
        System.out.println(leetCode0640.solveEquationNew("2x=x"));
        System.out.println(leetCode0640.solveEquationNew("x=x+2"));
        System.out.println(leetCode0640.solveEquationNew("-x=-1"));


    }
}
